package com.jixiang.tracedata.ibmmq;

import com.jixiang.tracedata.common.EventName;
import com.jixiang.tracedata.model.Error.OperationErr;
import com.jixiang.tracedata.model.flight.Msg;
import com.jixiang.tracedata.service.TraceService;
import com.jixiang.tracedata.util.HxCompressCode;
import com.jixiang.tracedata.util.StringOperate;
import com.jixiang.tracedata.util.XmlToBean;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;

/**
 * @author quanb
 * @date 2018-09-10
 */
@Component
public class IbmMqListener implements MessageListener {

	private Logger log = LoggerFactory.getLogger("IbmMqListener");

	@Autowired
	private TraceService traceService;

	/**
	 * 监听ibm mq queue消息
	 * @param msg
	 */
	@Override
	public void onMessage(Message msg) {
		//接收航信原始数据
		String res = "";
		try {
			res = uncompressXml(msg);
			//航信原始数据有多行，取最后一行xml报文
			String xmlContent = StringOperate.getXmlForMultiLine(res);

			//判断是否是航班事件，还是旅客事件
			String eventAffiliation = StringOperate.eventAffiliation(xmlContent);
			if(eventAffiliation.equals(EventName.FLIGHT_EVENT)){
				Msg flightMsg = XmlToBean.toBean(Msg.class, xmlContent);
				traceService.addFlightTraceMsg(xmlContent, flightMsg);
			}else if(eventAffiliation.equals(EventName.TRAVELLER_EVENT)){
				com.jixiang.tracedata.model.traveller.Msg travellerMsg = XmlToBean.toBean(com.jixiang.tracedata.model.traveller.Msg.class, xmlContent);
				traceService.addTravellerTraceMsg(xmlContent, travellerMsg);
			}else {
				log.info("该事件无任何归属！！！");
			}
		} catch (JMSException e) {
			//JMS错误
			addOperateErr(res, "JMSException");
		} catch (UnsupportedEncodingException e) {
			//解压缩错误
			addOperateErr(res, "UnsupportedEncodingException");
		}
	}

	/**
	 * 操作错误记录日志
	 * @param xml 原始xml
	 * @param process 操作步骤
	 */
	private void addOperateErr(String xml, String process){
		//操作错误实体
		OperationErr operationErr = new OperationErr();
		operationErr.setErrorXml(xml);
		operationErr.setProcess(process);
		traceService.addOperateErr(operationErr);
	}

	/**
	 * 解压缩航信原始压缩报文
	 * @return
	 */
	private String uncompressXml(Message msg) throws JMSException, UnsupportedEncodingException {
		//返回值
		String retStr = "";
		//字节数组
		byte[] byteArray;
		if (msg instanceof BytesMessage) {
			BytesMessage jmsByteMsg = (BytesMessage) msg;
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] bytes = new byte[32];
			for (int bytesRead = 0; (bytesRead = jmsByteMsg.readBytes(bytes, 32)) != -1; ) {
				bos.write(bytes, 0, bytesRead);
			}
			byteArray = bos.toByteArray();
			//调用航信的解压算法
			byte[] bytesMsg = HxCompressCode.unCompressMsg(byteArray);
			retStr = new String(bytesMsg, "UTF-8");
		}
		return retStr;
	}

}
